package com.core.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.core.base.dao.BaseHibernateDao;
import com.core.model.Menus;
import com.core.model.MenusCombineRoles;
import com.core.util.CollectionUtils;

/**
 * 菜单DAO
 * 
 * @author pt-chenqw
 *
 */
@Repository("menuDAO")
public class MenuDAO extends BaseHibernateDao<Menus> {
	
	@Resource(name="menusCombineRolesDAO")
	private MenusCombineRolesDAO menusCombineRolesDAO;
	
	/**
	 * 查询父菜单
	 * @return
	 */
	public List<Menus> getParentMenus(Integer roleID){
		List<MenusCombineRoles> combineRoles = menusCombineRolesDAO.getMenusCombineRolesByRoleID(roleID);
		List<Menus> menus = new ArrayList<>();
		if(CollectionUtils.notEmpty(combineRoles)){
			for (MenusCombineRoles menusCombineRole : combineRoles) {
				Menus menu = getMenuByMenuID(menusCombineRole.getMenuID());
				if(menu.getParentID() == 0){
					menus.add(menu);
				}
			}
		}
		return menus;
	}
	
	/***
	 * 查询父菜单
	 * @return
	 */
	public List<Menus> getParentMenus(){
		String hql = "from Menus where parentID = 0 and status = 1";
		return find(hql);
	}
	
	/**
	 * 查询指定角色所属子菜单
	 * @param parentid
	 * @return
	 */
	public List<Menus> geSubMenusByParentID(long parentid,Integer roleID){
		String hql = "from Menus where parentID = ? and status = 1";
		List<Menus> menus = find(hql, parentid);
		List<Menus> menusList = new ArrayList<>();
		if(CollectionUtils.notEmpty(menus)){
			for (Menus menu : menus) {
				if(roleID == null){
					return menus;
				}
				MenusCombineRoles menusCombineRoles = menusCombineRolesDAO.getMenusCombineRolesByMenuIDAndRoleID(menu.getId(), roleID);
				if(menusCombineRoles != null){
					menusList.add(menu);
				}
			}
		}
		return menusList;
	}
	
	/**
	 * 添加菜单
	 * @param menu
	 */
	public void addMenu(Menus menu){
		menu.setCreatetime(new Date());
		menu.setUpdatetime(new Date());
		menu.setSequence("1");
		save(menu);
	}
	
	/**
	 * 更新菜单信息
	 * @param menu
	 */
	public void updateMenu(Menus menu){
		Menus menus = getMenuByMenuID(menu.getId());
		menus.setMenu_zh_name(menu.getMenu_zh_name());
		menus.setMenu_en_name(menu.getMenu_en_name());
		menus.setParentID(menu.getParentID());
		menus.setUrl(menu.getUrl());
		menus.setMenuClass(menu.getMenuClass());
		menus.setStatus(menu.getStatus());
		menus.setUpdatetime(new Date());
		update(menus);
	}

	/**
	 * 根据菜单ID查询菜单(包含父菜单)
	 * @param menuID
	 * @return
	 */
	public Menus getMenuByMenuID(Integer menuID){
		String hql = "from Menus where id = ?";
		return findOne(hql, menuID);
	}
	
	/**
	 * 批量删除
	 * @param ids
	 */
	public void deleteMenusByMenuIDS(String ids){
		String[] menuIDS = ids.split(",");
		for (String menuID : menuIDS) {
			Integer id = Integer.parseInt(menuID);
			deleteMenuByMenuID(id);
		}
	}
	
	/**
	 * 删除菜单及其子菜单
	 * @param menuID
	 */
	public void deleteMenuByMenuID(Integer menuID){
		Menus menu = getMenuByMenuID(menuID);
		//菜单列表
		List<Menus> menus = new ArrayList<>();
		//菜单角色列表
		List<MenusCombineRoles> combineRoles = new ArrayList<>();
		//0为父菜单,删除其子菜单
		if(menu != null && menu.getParentID() == 0){
			menus = geSubMenusByParentID(menu.getId(), null);
			for (int i = 0; i < menus.size(); i++) {
				MenusCombineRoles menusCombineRoles = menusCombineRolesDAO.findMenusCombineRolesByMenuID(menus.get(i).getId());
				if(menusCombineRoles != null){
					combineRoles.add(menusCombineRoles);
				}
			}
		}
		menus.add(menu);
		MenusCombineRoles menusCombineRole = menusCombineRolesDAO.findMenusCombineRolesByMenuID(menuID);
		if(menusCombineRole != null){
			combineRoles.add(menusCombineRole);
			menusCombineRolesDAO.deleteBatch(combineRoles);
		}
		deleteBatch(menus);
	}
	
	/**
	 * 根据菜单Code查询菜单信息
	 * @param menuCode
	 * @return
	 */
	public Menus getMenusByCode(String menuCode){
		String hql = "from Menus where menuCode = ?";
		return findOne(hql, menuCode);
	}
	
	/**
	 * 根据请求链接查询菜单信息
	 * @param actionUrl
	 * @return
	 */
	public Menus getMenusByActionUrl(String actionUrl){
		String hql = "from Menus where url = ?";
		return findOne(hql, actionUrl);
	}
	
}
